package view
{
	/**
	 *
	 *@author CrazyChen 
	 *2013-4-1
	 */	
	import flash.display.MovieClip;
	import flash.events.Event;
	
	import manager.EffectName;
	import manager.ObjectPool;
	import manager.SoundManage;
	
	import utils.MCDeleteBlack;
	
	import view.base.ClearView;
	import view.effect.BaseEffect;

	public class ExplodeView extends ClearView
	{		
		private var bmAry:Array;
		
		private var mcAry:Array;
		
		public function ExplodeView()
		{
			super();
			this.mouseChildren = false;
			this.mouseEnabled = false;
			bmAry=[];
			mcAry = [];
		}
		
		public function createExplode(name:String,_x:int=0,_y:int=0,_rotation:int=0,scale:Number = 0):void{
			var cls:Class;
	        if(name == 'D'){
			    cls= EffectName.getMovieClipCls(name);
				var mc:MovieClip = new cls();
				mcAry.push(mc);
				mc.x = _x;
				mc.y = _y;
				mc.rotation = _rotation;
				this.addChild(mc);
				mc.stop();
				SoundManage.getInstance().play(SoundManage.HAND_GRENADE);
			}else{
				var effectCls:Class = EffectName.getEffectCls(name);
				var be:BaseEffect = ObjectPool.getObj(effectCls) as BaseEffect;
				if(!be.bmdAry){
					cls = EffectName.getMovieClipCls(name);
					be.bmdAry= MCDeleteBlack.getNewMc(new cls());
				}
				be.bitmapData = be.bmdAry[0];
				be.name = name;
				be.scale = scale;
				if(scale!=0)be.scaleX = be.scaleY = scale;
				be.x = _x-be.width/2;
				be.y = _y-be.height/2;
				be.rotation = _rotation;
				be.curPlayIndex=0;
				this.addChild(be);
				bmAry.push(be);
				SoundManage.getInstance().play(be.sndName);
			}
		}
		
		override protected function loop(event:Event):void{
			var i:int=0;
            if(bmAry.length){
				var be:BaseEffect;
				for(i;i<bmAry.length;i++){
					be= bmAry[i];
					be.curPlayIndex++;
					if(be.curPlayIndex>be.bmdAry.length-1){
						this.removeChild(be);
						ObjectPool.putObj(be);
						bmAry.splice(i,1);
						continue;
					}
					be.bitmapData = be.bmdAry[be.curPlayIndex];
					if(be.scale!=0)be.scaleX = be.scaleY = be.scale;
					var xx:int = be.x+be.width/2;
					var yy:int = be.y+be.height/2;
					be.x = xx-be.width/2;
					be.y = yy -be.height/2;
				}
			}
			i=0;
			if(mcAry.length){
				for(i;i<mcAry.length;i++){
					mcAry[i].nextFrame();
					if(mcAry[i].currentFrame == mcAry[i].totalFrame-1){
						this.removeChild(mcAry[i]);
						mcAry.splice(i,1);
						i--
					}
				}
			}
		}
		
	
	}
}